<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 李邦琦 <1536549251@qq.com>
// +----------------------------------------------------------------------

namespace app\crm\model;
use think\Db;
use think\Model;
use think\Cache;
use think\Loader;

class Datatask extends Model
{
    /**
     * 获取任务列表
     * @param  string $where 获取条件
     * @return array         获取到的数据
     */
    public function dataTaskListData ($where)
    {
        $page = $_REQUEST["page"];
        $limit = $_REQUEST["limit"];
        $users = Cache::get('users');
        $count = Db::name('vip_data_task')->where($where)->count();
        // 查询状态为1的用户数据 并且每页显示5条数据
        $dataTaskListData = Db::name('vip_data_task')->where($where)->limit(($page-1)*$limit,$limit)->order('createtime DESC')->select();
        foreach ($dataTaskListData as &$val) {
            $val['createuser'] = $users[$val['createuser']]['cn_name'];
            $val['username'] = $users[$val['username']]['cn_name'];
        }
        $rowsList = count($dataTaskListData) ? $dataTaskListData : false;
        $arrData["code"] = 0;
        $arrData["msg"] = '';
        $arrData["count"] = $count;
        $arrData["data"] = $rowsList;
        // print_r($arrData);exit;
        return $arrData;
    }


    /**
     * 获取任务的客户列表
     * @param  string $where 获取条件
     * @return array         获取到的数据
     */
    public function dataTaskDetailData ($where)
    {
        $page = $_REQUEST["page"];
        $limit = $_REQUEST["limit"];
        $users = Cache::get('users');
        $count = Db::name('vip_data_customer')->where($where)->count();
        // 查询状态为1的用户数据 并且每页显示5条数据
        $dataTaskDetailData = Db::name('vip_data_customer')->where($where)->limit(($page-1)*$limit,$limit)->order('addtime ASC')->select();
        foreach ($dataTaskDetailData as &$val) {
            $val['username'] = $users[$val['username']]['cn_name'];
            $val['task_status'] = $val['task_status'] == 'N'?'未完成':'已完成';
        }
        $rowsList = count($dataTaskDetailData) ? $dataTaskDetailData : false;
        $arrData["code"] = 0;
        $arrData["msg"] = '';
        $arrData["count"] = $count;
        $arrData["data"] = $rowsList;
        // print_r($arrData);exit;
        return $arrData;
    }


    /**
     * 手动确定完成数据任务
     * @param  string $id               数据在任务中的id
     * @param  string $customer_code    客户资料
     * @return json 完成状态
     */
    public function completeDataTask ($id, $customer_code)
    {
        // print_r($customer_code);exit;
        $result1 = Db::name('vip_data_customer')->where('id="'.$id.'"')->update(array('task_status'=>'Y'));
        $result2 = Db::name('vip_customer_basic')->where('customer_code="'.$customer_code.'"')->update(array('data_task'=>null));
        if ($result1 && $result2) {
            echo json_encode(array('success'=>true,'msg'=>'操作成功！'));
        } else {
            echo json_encode(array('msg'=>'操作失败！'));
        }
    }

}